package com.example.mvc_thymeleaf.controller;


import com.example.mvc_thymeleaf.model.Orders;
import com.example.mvc_thymeleaf.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @GetMapping("/")
    public String viewHomePage(Model model) {
        return findPaginated(1, "status", "asc", model);
    }

    //添加数据
    @GetMapping("/showNewOrdersForm")
    public String showNewOrdersForm(Model model) {
        // 创建模型属性来绑定表单数据
        Orders orders = new Orders();
        //addAttribute方法用于向模型（Model）中添加属性（Attribute）供视图（View）使用。
        //创建了一个名为orders，值为一个orders对象的属性
        model.addAttribute("orders", orders);
        //返回到net_orders 视图
        return "new_orders";
    }

    // 将员工保存到数据库
    @PostMapping("/saveOrders")
    public String saveOrders(@ModelAttribute("orders") Orders orders) {

        ordersService.saveOrders(orders);
        return "redirect:/";
    }
    //更新数据
    @GetMapping("/showFormForUpdate/{id}")
    //@PathVariable 注解来获取在 URL 中动态传递的参数
    public String showFormForUpdate(@PathVariable(value = "id") long id, Model model) {

        // 从服务中获取员工
        Orders orders = ordersService.getOrdersById(id);

        // 将employee设置为模型属性以预填充表单
        model.addAttribute("orders", orders);
        return "update_orders";
    }
    //删除数据
    @GetMapping("/deleteOrders/{id}")
    public String deleteOrders(@PathVariable(value = "id") long id) {

        // call delete employee method
        this.ordersService.deleteOrdersById(id);
        return "redirect:/";
    }

    //通过状态查询
    @GetMapping("/find")
    public String findStatus(@RequestParam("status") String status, Model model){
        List<Orders> byStatus = ordersService.findByStatus(status);
        model.addAttribute("byStatus", byStatus);
        return "find_status";
    }

    //分页排序
    @GetMapping("/page/{pageNo}")
    public String findPaginated(@PathVariable (value = "pageNo") int pageNo,
                                //@RequestParam 注解可以将这些查询参数绑定到处理器方法的参数上，从而方便获取和处理这些参数。
                                @RequestParam("sortField") String sortField,
                                @RequestParam("sortDir") String sortDir,
                                Model model) {
        int pageSize = 8;

        Page<Orders> page = ordersService.findPaginated(pageNo, pageSize, sortField, sortDir);

        List<Orders> listOrders = page.getContent();

        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems", page.getTotalElements());

        model.addAttribute("sortField", sortField);
        model.addAttribute("sortDir", sortDir);
        model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");

        model.addAttribute("listOrders", listOrders);
        return "index";
    }
}
